﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace SIN.Client
{
    public partial class MainFrm : Form
    {       
        public MainFrm()
        {
            InitializeComponent();
        }

        public void setStatus(string aStatus)
        {
            toolStripStatusLabel.Text = DateTime.Now + " >> " + aStatus;
        }

        public void listDownloaded()
        {
            Program.aoAddonsListController.parseAddonsList();
        }

        public void infoDownloaded()
        {
            Program.moreInfoForm.Show();
        }

        public void startUpdate()
        {
            this.addonsTableView.Rows.Clear();
            this.setStatus("Preparing for download list");
            Program.webClientModel.downloadList();
        }

        public void getAddonInfo() 
        {
            this.setStatus("Preparing for download info");
            Program.webClientModel.downloadInfo(Program.addonsListModel.selectedAddon());
        }

        public void setIdle(bool aIdle)
        {
            if (aIdle)
            {
                this.moreInfoLabel.Visible = false;
                this.addonDescription.Visible = false;
                this.Cursor = Cursors.WaitCursor;
            }
            else
            {
                this.Cursor = Cursors.Default;
                this.moreInfoLabel.Visible = true;
                this.addonDescription.Visible = true;
            }
        }

        private void updateListButton_Click(object sender, EventArgs e)
        {
            this.startUpdate();
        }

        private void MainFrm_Load(object sender, EventArgs e)
        {
            this.setStatus("SIN.Client started");
            Program.aoAddonsListController.checkAddonsList();
        }

        public void setAddonsList() {

            string aAddons = Program.aoAddonsListController.getAddonsList();
            string [] aAddonsRows = aAddons.Split('\n');
            int i = 0;
            string aAddonName = "";                    
            foreach (string aAddonsRow in aAddonsRows) 
            {               
                    i++;
                    if (i == 1) { aAddonName = aAddonsRow; }
                    if (i == 2) {
                                  DataGridViewImageCell aCell1 = new DataGridViewImageCell();
                                  DataGridViewTextBoxCell aCell2 = new DataGridViewTextBoxCell();
                                  DataGridViewTextBoxCell aCell3 = new DataGridViewTextBoxCell();
                                  DataGridViewRow aRow = new DataGridViewRow();

                                  aCell1.Value = Resources.Images.addonNotInstalled;
                                  aCell1.ToolTipText = "Addon \""+aAddonName+"\" not installed yet";
                                  aCell2.Value = aAddonName;
                                  aCell3.Value = aAddonsRow;

                                  aRow.Cells.AddRange(aCell1, aCell2, aCell3);
                                  addonsTableView.Rows.Add(aRow); 
                                  i = 0; 
                    }                        
            }
            Program.aoAddonsListController.getAddonInfo(0);
        }

        public void setAddonDescription(string aAddonInfo)
        {
            string[] aAddonsInfoRows = aAddonInfo.Split('\t');
            Program.addonsListModel.setSelectedAddon(aAddonsInfoRows[0]); 

            addonDescription.Clear();
            addonDescription.SelectionColor = Color.Blue;            
            addonDescription.SelectionFont = new Font(this.Font,FontStyle.Bold);
            addonDescription.AppendText(aAddonsInfoRows[0]);
            addonDescription.SelectionColor = Color.Black;
            addonDescription.AppendText("\n");
            addonDescription.AppendText(aAddonsInfoRows[1]);
            addonDescription.Select(0, 0);

            this.setIdle(false);
        }

        private void addonsTableView_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            this.setIdle(true);
            Program.aoAddonsListController.getAddonInfo(e.RowIndex);
        }

        private void moreInfoLabel_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            Program.aoAddonInfoController.checkAddonInfo(Program.addonsListModel.selectedAddon());
        }

        private void updateAllButton_Click(object sender, EventArgs e)
        {
        }
        
    }
}
